//
//  PopoverAnimator.swift
//  WB
//
//  Created by leador on 17/3/21.
//  Copyright © 2017年 袁满. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {

    var presentedFrame:CGRect = CGRect.zero
    var presentedTransitionDuration:TimeInterval = 0.5
    
    lazy var isPresented:Bool = false
    
    var callBack: ((Bool)->())?
    
    init(callBack:@escaping ((Bool)->())) {
        self.callBack = callBack
    }
    
}

extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentationControl = YMPresentationController(presentedViewController: presented, presenting: presenting)
        presentationControl.presentedFrame = presentedFrame
        return presentationControl
        
    }
    
    //自定义弹出动画
    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        callBack!(isPresented)
        return self
    }
    
    //自定义消失动画
    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        callBack!(isPresented)
        return self
    }
    
}


extension PopoverAnimator : UIViewControllerAnimatedTransitioning {
    
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return presentedTransitionDuration
    }
    
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationedForPresent(transitionContext: transitionContext):animationedForDismiss(transitionContext: transitionContext)
        
    }
    
    func animationedForPresent(transitionContext: UIViewControllerContextTransitioning) {
        
        //UITransitionContextViewKey.to  获取弹出的view
        //UITransitionContextViewKey.from 获取消失的View
        
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        transitionContext.containerView.addSubview(presentedView)
        
        presentedView.transform = CGAffineTransform(scaleX: 1.0,y: 0)
        
        UIView.animate(withDuration:transitionDuration(using: transitionContext), animations: {
            
            presentedView.layer.anchorPoint = CGPoint(x:0.5,y:0)
            presentedView.transform = CGAffineTransform(scaleX: 1.0,y: 1.0)
            
        }) { (_) in
            transitionContext.completeTransition(true)
        }
        
    }
    
    func animationedForDismiss(transitionContext: UIViewControllerContextTransitioning) {
        
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            dismissView.transform = CGAffineTransform(scaleX:1.0,y:0.0001)
            
        }) { (_) in
            
            dismissView.removeFromSuperview()
            transitionContext.completeTransition(true)
            
        }
        
    }
    
}

